// pages/studentList/studentList.js
var request = require('../../operation/operation.js')
var klassModel = require('../../models/klass.js')
var klassSid;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    students:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    klassSid = options.klassSid
    this.getStudents(klassSid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 点击添加学生事件处理函数
   */
  onCreateStudent:function() {
    wx.navigateTo({
      url: '../editStudent/editStudent?klassSid=' + klassSid,
    })
  },

  onBindRelation:function(event) {
    var student = event.currentTarget.dataset.student
    getApp().globalData.param = student
    wx.navigateTo({
      url: '../editStudent/editStudent?studentSid=' + student.sid,
    })
  },

  /**
   * 获取班级学生信息
   */
  getStudents:function(klassSid) {
    var that = this;
    request.getRequest('/students?klassSid=' + klassSid, {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          that.setData({
            students:data.items
          })
        }
      }).catch(e => {
        console.log(e)
      })
  }
})